using System.IO;
using UnityEditor;
using UnityEngine;

namespace GameHelper.Config
{
    /// <summary>
    /// 配置编辑器代码更新
    /// </summary>
    public class ConfigCodeGenEditor
    {
        [MenuItem(ConfigCreatorConst.CONFIG_CREATE_PATH + ">>>更新Config创建代码<<<")]
        private static void RefreshConfigEditorCode()
        {
            string configName = "GameConfigCreator";
            string scriptPath = Application.dataPath + "/ConfigCreator/Configs/Editor/" + configName + ".cs";
            string classInfo = File.ReadAllText(Application.dataPath + "/ConfigCreator/CodeGen/Editor/GameConfigCreatorTemplate.txt");

            string createFunString =
                @"
            [MenuItem(ConfigCreatorConst.CONFIG_CREATE_PATH + nameof(#配置类名#))]
            private static void Create#配置类名#()
            {
                CreateConfigInstance<#配置类名#>();
            }";

            FileInfo[] fileInfos = GetDirectoryFile(Application.dataPath + "/ConfigCreator/Configs/");
            string[] classNames = new string[fileInfos.Length];
            string funInfos = "";

            for (int i = 0; i < fileInfos.Length; i++)
            {
                if (fileInfos[i].Name.EndsWith(".meta"))
                {
                    continue;
                }

                classNames[i] = fileInfos[i].Name;
                classNames[i] = classNames[i].Replace(".cs", "");
                string funInfo = createFunString.Replace("#配置类名#", classNames[i]);
                classNames[i] = funInfo;

                funInfos += ("\n" + funInfo);
            }

            classInfo = classInfo.Replace("#创建函数#", funInfos);

            if (!Directory.Exists(scriptPath))
            {
                UnityEngine.Debug.LogWarning($"Config脚本已存在，删除重建{scriptPath}");
                File.Delete(scriptPath);
            }

            FileStream file = new FileStream(scriptPath, FileMode.CreateNew);
            StreamWriter fileW = new StreamWriter(file, System.Text.Encoding.UTF8);
            fileW.Write(classInfo);
            fileW.Flush();
            fileW.Close();
            file.Close();

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            UnityEngine.Debug.LogWarning("更新Config创建代码");
        }

        private static FileInfo[] GetDirectoryFile(string path)
        {
            if (Directory.Exists(path))
            {
                DirectoryInfo direction = new DirectoryInfo(path);
                FileInfo[] files = direction.GetFiles("*", SearchOption.TopDirectoryOnly);
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Name.EndsWith(".meta"))
                    {
                        continue;
                    }

                    Debug.Log("Name : " + files[i].Name); //文件名
                    Debug.Log("FullName : " + files[i].FullName); //根目录下的文件的目录
                    Debug.Log("DirectoryName : " + files[i].DirectoryName); //根目录
                }

                return files;
            }

            return null;
        }
    }
}